package com.project.weimi.activity;

import java.util.HashMap;
import java.util.Map;

import com.project.weimi.R;
import com.project.weimi.application.Constant;
import com.project.weimi.entity.response.ResponseBean;
import com.project.weimi.interfaces.MyCallBack;
import com.project.weimi.utils.GsonUtil;
import com.project.weimi.utils.NetUtils;
import com.project.weimi.utils.SetClickListener;
import com.project.weimi.utils.XUtil;
import com.project.weimi.widget.ToastMessage;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * 修改性别Activity
 * @author Administrator
 *
 */
public class SexActivity extends BaseActivity{

	private RelativeLayout rl_male,rl_female;
	private TextView tv_male,tv_female;
	private ImageView img_check_male,img_check_female;
	private String userPhone;
	private String sex;
	String mSex;
	
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentViewWithBar(R.layout.activity_sex, "性别", true, " 保存");
		
		// 获得从个人信息页面传来的性别值
		mSex = getIntent().getStringExtra("sex");
		
		initView();
		
	}
	
	private void initView(){
		rl_male = (RelativeLayout)findViewById(R.id.rl_male);
		rl_female = (RelativeLayout)findViewById(R.id.rl_female);
		tv_male = (TextView)findViewById(R.id.tv_male);
		tv_female = (TextView)findViewById(R.id.tv_female);
		img_check_male = (ImageView)findViewById(R.id.img_check_male);
		img_check_female = (ImageView)findViewById(R.id.img_check_female);
		
		if(TextUtils.isEmpty(mSex)){
			img_check_male.setVisibility(View.GONE);
			img_check_female.setVisibility(View.GONE);
		} else if ("男".equals(mSex)){
			img_check_male.setVisibility(View.VISIBLE);
			img_check_female.setVisibility(View.GONE);
		} else if ("女".equals(mSex)){
			img_check_male.setVisibility(View.GONE);
			img_check_female.setVisibility(View.VISIBLE);
		} 
		
		// 监听
		SetClickListener.setClickListener(listener, rl_male,rl_female,bar.getRightBar());
	}
	
	private OnClickListener listener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			
			switch (v.getId()) {
			case R.id.rl_male: // 男
				if(img_check_female.getVisibility() == View.VISIBLE){
					ToastMessage.showMyToast(SexActivity.this, "性别修改后不可再次修改");
					return;
				} 
				else {
					img_check_male.setVisibility(View.VISIBLE);
					img_check_female.setVisibility(View.GONE);
				}
				
				break;
			case R.id.rl_female: // 女
				if(img_check_male.getVisibility() == View.VISIBLE){
					ToastMessage.showMyToast(SexActivity.this, "性别修改后不可再次修改");
					return;
				} 
				else {
					img_check_male.setVisibility(View.GONE);
					img_check_female.setVisibility(View.VISIBLE);
				}
				
				break;
			case R.id.right_bar: // 保存

				if(!NetUtils.isConnected(SexActivity.this)){
					ToastMessage.showToast(SexActivity.this, "网络不给力，请检查网络设置");
				} else {
					initHttp();
				}
				
				break;
			}
		}
	};
	
	private void initHttp(){
		
		if(img_check_male.getVisibility() == View.GONE && img_check_female.getVisibility() == View.GONE){
			ToastMessage.showMyToast(SexActivity.this, "请选择您的性别");
			return;
		}
		if(!TextUtils.isEmpty(mSex)){
			ToastMessage.showMyToast(SexActivity.this, "性别修改后不可再次修改");
			return;
		}
		if(TextUtils.isEmpty(userPhone)){
			return;
		}
		
		if(img_check_male.getVisibility() == View.VISIBLE){
			ToastMessage.showMyToast(SexActivity.this, "保存"+tv_male.getText());
			sex = tv_male.getText().toString();
		}
		if(img_check_female.getVisibility() == View.VISIBLE){
			ToastMessage.showMyToast(SexActivity.this, "保存"+tv_female.getText());
			sex = tv_female.getText().toString();
		}
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("memberId", userPhone);
		map.put("sex", sex);
		
		XUtil.post(Constant.CHANGE_SEX, map, new MyCallBack<String>(){
			
			@Override
			public void onSuccess(String result) {
				super.onSuccess(result);
				if(null != result && !"".equals(result)){
					
					ResponseBean bean = (ResponseBean) GsonUtil.jsonToBean(
							result, ResponseBean.class);
					if(null != bean){
						String str = bean.getResult();
						if(!TextUtils.isEmpty(str) && "1".equals(str)){
							ToastMessage.showMyToast(SexActivity.this, "修改成功");
							Intent intent = new Intent();
							intent.putExtra("sex", sex);
							setResult(1, intent);
							finish();
						}
					}
				}
			}
			
			@Override
			public void onError(Throwable ex, boolean isOnCallback) {
				super.onError(ex, isOnCallback);
			}
		});
	}
	
	@Override
	protected void onStart() {
		super.onStart();
		if(null != spHelper && !TextUtils.isEmpty(memberId)){
			//Log.e("TAG", "onStart----SexActivity前memberId===---"+memberId);
			userPhone = memberId;
			//Log.e("TAG", "onStart----SexActivity后userPhone===---"+userPhone);
		}
	}
	
	@Override
	void setStatusBar(int statusBarColorRes) {
		super.setStatusBar(R.color.transparent_3);
	}
	
	
}
